/**************************************************************************************
*  SerDemo.java
*  Program demonstrating the use of serial ports with LPC21xx
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Created by:
*      RTJ Computing
**************************************************************************************/
import java.util.*;
import lpc2xxx.hw.*;
import lpc2xxx.comm.*;


class SerDemo
{
    static void main(String[] args)
    {
        int baud = 115200;
        Console.println("");
        Console.println("LPC21xx UART port test program.");
        Console.println("Connect UART0 or UART1 to PC and start the terminal program");
        Console.println("configured to " + baud + ",N,8,1.\r\n");
        Console.println("Ensure that the selected UART will not conflict with the UART used for console in/out.\r\n");
        Console.println("Press ");
        Console.println("  0   - to activate UART0 port");
        Console.println("  1   - to activate UART1 port");
        Console.println("  ESC - to exit");

        UART port = null;
        try
        {
            while (true)
            {
                char ch = Console.getKey();
                if (ch == 0x1B)
                    return;
                else if (ch == '0')
                {
                    port = new UART(UART.UART0);
                    break;
                }
                else if (ch == '1')
                {
                    port = new UART(UART.UART1);
                    break;
                }
            }
            if (port.open(baud, UART.PAR_NONE, UART.BITS_8, UART.STOP_1))
            {
                TermThread term = new TermThread(port);
                term.start();
                while (true)
                {
                    if (Console.getKey() == 0x1B || term.terminated) 
                    {
                        term.terminate();
                        break;
                    }
                    Thread.yield();
                }
            }
            else
                Console.println("Unable to open UART port");
        }
        catch (InterruptedException exc) {}
        catch (Exception exc)
        {
            Console.println(exc.getMessage());
        }
        finally
        {
            if (port != null)
                port.close();
        }
        Console.println("Main exit");
    }

}

